package com.inhim.pjhim.adapter;

import android.content.Context;
import android.graphics.Paint;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.inhim.pjhim.R;
import com.inhim.pjhim.entity.ReaderTagList;

import org.yczbj.ycrefreshviewlib.adapter.RecyclerArrayAdapter;
import org.yczbj.ycrefreshviewlib.holder.BaseViewHolder;


/**
 * Created by hsmacmini on 2018/4/8.
 */

public class DialogFoundRecViewAdapter extends RecyclerArrayAdapter<ReaderTagList.List> {
    private Context mContext;
    final static int ITEMS_PER_PAGE = 4;
    private int temp;
    private boolean mIshow;
    private ItemOnClickListener mItemOnClickListener;
    public DialogFoundRecViewAdapter(Context context ){
        super(context);
        this.mContext=context;
    }

    @Override
    public BaseViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) {
        final ViewHolder holder;
        View view = null;
        if (view == null) {
            view= LayoutInflater.from(parent.getContext()).inflate(R.layout.item_dialog_found_rec_view,parent,false);
            holder = new ViewHolder(view,viewType);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                temp = viewType;
                //刷新界面 notify 通知Data 数据set设置Changed变化
                //在这里运行notifyDataSetChanged 会导致下面的onBindViewHolder 重新加载一遍
                /*holder.tv_name.setTextColor(mContext.getResources().getColor(R.color.color_D5802E));
                holder.tv_name.setBackgroundResource(R.drawable.backgroud_thumb);*/
                notifyDataSetChanged();
                mItemOnClickListener.onClick(viewType);
            }
        });
        return holder;
    }

    public void setShowAllView(boolean isShow,int temp){
        this.temp=temp;
        mIshow=isShow;
    }

    public boolean getShowAllView(){
        return mIshow;
    }
    class ViewHolder extends BaseViewHolder<ReaderTagList.List>{
        TextView tv_name;
        LinearLayout constran;
        LinearLayout lin_all;
        TextView tv_name_all;
        ImageView ic_right_all;
        public ViewHolder(@NonNull View itemView,int position) {
            super(itemView);
            tv_name = itemView.findViewById(R.id.tv_name);
            constran=itemView.findViewById(R.id.constran);
            lin_all=itemView.findViewById(R.id.lin_all);
            tv_name_all=itemView.findViewById(R.id.tv_name_all);
            ic_right_all=itemView.findViewById(R.id.ic_right_rown);
        }

        @Override
        public void setData(ReaderTagList.List data) {
            super.setData(data);
            //动态计算屏幕宽度 一行显示四个item
            Paint mTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
            mTextPaint.setTextSize(mContext.getResources().getDimensionPixelSize(R.dimen.text_size_14));
            int textWidth = (int) mTextPaint.measureText(data.getName());
            int margin=mContext.getResources().getDimensionPixelSize(R.dimen.base_dimen_30);
            LinearLayout.LayoutParams params;
            if(data.getName().equals(mContext.getResources().getString(R.string.see_all))){
                tv_name.setVisibility(View.GONE);
                lin_all.setVisibility(View.VISIBLE);
                tv_name_all.setTextColor(mContext.getResources().getColor(R.color.white));
                lin_all.setBackgroundResource(R.drawable.backgroud_all_view_item);
                params = new LinearLayout.LayoutParams((textWidth+margin+ic_right_all.getWidth()-mContext.getResources().getDimensionPixelSize(R.dimen.base_dimen_0)),
                        ViewGroup.LayoutParams.MATCH_PARENT);
                tv_name_all.setLayoutParams(params);
                tv_name_all.setText(data.getName());
            }else{
                tv_name.setVisibility(View.VISIBLE);
                lin_all.setVisibility(View.GONE);
                if(temp==getAdapterPosition()){
                    tv_name.setTextColor(mContext.getResources().getColor(R.color.color_D5802E));
                    tv_name.setBackgroundResource(R.drawable.backgroud_found_label);
                }else{
                    tv_name.setTextColor(mContext.getResources().getColor(R.color.white));
                    tv_name.setBackgroundResource(R.drawable.backgroud_dialog_found_item);
                }
                 params = new LinearLayout.LayoutParams((textWidth+margin),
                        ViewGroup.LayoutParams.MATCH_PARENT);
                tv_name.setLayoutParams(params);
                tv_name.setText(data.getName());
            }

        }
    }

    public void setItemOnClickListener(ItemOnClickListener itemOnClickListener){
        mItemOnClickListener=itemOnClickListener;
    }
    public interface ItemOnClickListener{
        void onClick(int position);
    }


    public void notfyPosition(int adapterPsition){
        temp=adapterPsition;
        notifyDataSetChanged();
    }
}
